package com.feri.fitness.cs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feri.fitness.cs.dao.MoodCollectDao;
import com.feri.fitness.cs.domain.MoodCollect;
import com.feri.fitness.cs.service.intf.MoodCollectService;
import com.feri.fitness.common.vo.R;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 千锋郑州Java学科欢迎你
 * @program: Fitness
 * @description: 业务接口实现层
 * @author: Feri
 * @create: 2023-01-12 08:59:28
 */
@Service
public class MoodCollectServiceImpl implements MoodCollectService{
    /**
    *注入持久层对象*/
    @Resource
    private MoodCollectDao dao;

    @Override
    public R add(int uid, int mid) {
        //1.验证
        if(mid>0){
            //2.查询
            QueryWrapper<MoodCollect> wrapper=new QueryWrapper<>();
            wrapper.eq("uid",uid);
            wrapper.eq("mid",mid);
            MoodCollect collect=dao.selectOne(wrapper);
            //3.验证是否存在
            if(collect!=null){
                //取消收藏
                dao.deleteById(collect.getId());
                return R.ok();
            }else {
                //原来没有，本次就是新增
                if(dao.insert(new MoodCollect(uid,mid))>0){
                    return R.ok();
                }
            }
        }
        return R.fail();
    }

    @Override
    public R query(int uid) {
        //实际上多表联查 懒省事单表

        return R.ok(dao.selectList(new QueryWrapper<MoodCollect>().eq("uid",uid)));
    }
}